package com.jieao.backend.controller.content;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jieao.common.annotation.Log;
import com.jieao.common.core.controller.BaseController;
import com.jieao.common.core.domain.AjaxResult;
import com.jieao.common.core.domain.Ztree;
import com.jieao.common.enums.BusinessType;
import com.jieao.common.utils.StringUtils;
import com.jieao.content.domain.Catalog;
import com.jieao.content.dto.CatalogDto;
import com.jieao.content.service.ICatalogService;
import com.jieao.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Description
 * @Author weifanl
 * @Date2020/5/23 21:58
 **/
@Controller
@RequestMapping("/content/catalog")
public class CatalogController extends BaseController {

    private String prefix = "backend/content/catalog";

    @Autowired
    private ICatalogService catalogService;

    @RequiresPermissions("content:catalog:view")
    @GetMapping()
    public String catalog(String module, ModelMap mmap) {
        mmap.put("module", module);
        return prefix + "/catalog";
    }

    @RequiresPermissions("content:catalog:list")
    @PostMapping("/list")
    @ResponseBody
    public List<CatalogDto> list(CatalogDto catalog) {
        List<CatalogDto> catalogList = catalogService.selectCatalogList(catalog);
        return catalogList;
    }

    /**
     * 新增目录
     */
    @GetMapping("/add/{parentId}")
    public String add(@PathVariable("parentId") String parentId, ModelMap mmap) {
        String[] split = parentId.split("-");
        mmap.put("module",split[1]);
        CatalogDto catalogDto;

        if(StringUtils.isNotEmpty(split[0])){
            catalogDto = catalogService.selectCatalogById(Long.valueOf(split[0]));
        }else{
            Catalog one = catalogService.getOne(new QueryWrapper<Catalog>().lambda()
                    .eq(Catalog::getModule, split[1]).isNull(Catalog::getParentId).eq(Catalog::getStatus, 1));
            catalogDto = catalogService.selectCatalogById(one.getId());
        }
        mmap.put("catalog",catalogDto);
        return prefix + "/add";
    }

    /**
     * 新增保存目录
     */
    @Log(title = "目录管理", businessType = BusinessType.INSERT)
    @RequiresPermissions("content:catalog:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated Catalog catalog) {
        catalog.setCreateBy(ShiroUtils.getLoginName());
        catalog.setCreateTime(new Date());
        return toAjax(catalogService.insertCatalog(catalog));
    }

    /**
     * 修改
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        CatalogDto catalog = catalogService.selectCatalogById(Long.valueOf(id));
        mmap.put("catalog", catalog);
        return prefix + "/edit";
    }

    /**
     * 保存
     */
    @Log(title = "目录管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("content:catalog:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Catalog catalog) {
        if (catalog.getParentId().equals(catalog.getId())) {
            return error("修改目录'" + catalog.getCatalogName() + "'失败，上级目录不能是自己");
        }
        return toAjax(catalogService.updateCatalog(catalog));
    }

    /**
     * 删除
     */
    @Log(title = "目录管理", businessType = BusinessType.DELETE)
    @RequiresPermissions("content:catalog:remove")
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id) {
        if (catalogService.selectCatalogCount(id) > 0) {
            return AjaxResult.warn("存在下级目录,不允许删除");
        }
        return toAjax(catalogService.deleteCatalogById(id));
    }

    /**
     * 选择目录树
     */
    @GetMapping("/selectCatalogTree/{id}")
    public String selectCatalogTree(@PathVariable("id") String id, ModelMap mMap) {
        String[] split = id.split("-");
        mMap.put("module",split[1]);
        CatalogDto catalogDto = new CatalogDto();
        if(StringUtils.isNotEmpty(split[0])){
            catalogDto = catalogService.selectCatalogById(Long.valueOf(split[0]));
        }
        mMap.put("catalog", catalogDto);
        return prefix + "/tree";
    }

    /**
     * 加载目录（数据权限）列表树
     */
    @GetMapping("/catalogTreeData")
    @ResponseBody
    public List<Ztree> catalogTreeData() {
        List<Ztree> ztrees = catalogService.selectCatalogTree();
        return ztrees;
    }

    /**
     * 加载目录列表树
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData(CatalogDto catalogDto) {
        List<Ztree> ztrees = catalogService.selectCatalogTree(catalogDto);
        return ztrees;
    }
}
